<template>
  <BasicModal title="改价" v-bind="$attrs" @ok="handleOk" @register="registerModal">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script setup lang="ts">
  import mcApi from 'mc-c2b2m-operator-api';
  import { useMessage } from '@/hooks/web/useMessage';
  import { BasicForm, useForm } from '@/components/Form';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { changePriceFormSchema } from '@/views/order/salesOrder/data';

  const { OrderModule } = mcApi;
  const { SalesOrderApi } = OrderModule;

  const [registerForm, { resetFields, validate, setFieldsValue }] = useForm({
    labelWidth: 100,
    baseColProps: { span: 22 },
    showActionButtonGroup: false,
    schemas: changePriceFormSchema,
  });

  const [registerModal, { setModalProps }] = useModalInner(async (data) => {
    await resetFields();
    await setFieldsValue({
      ...data,
      adjustmentMethod: 2,
    });
  });

  const emits = defineEmits(['success']);
  const { createMessage } = useMessage();

  async function handleOk() {
    try {
      const values: any = await validate();
      const { resultAmount } = values;
      if (resultAmount < 0) {
        createMessage.error('调整后金额不能小于0！');
        return;
      }
      setModalProps({ confirmLoading: true });
      await SalesOrderApi.modifySalesOrderItemAmount(values);
      createMessage.success('改价成功！');
      emits('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style scoped lang="less"></style>
